package huffmanTree;


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Aaron
 * @desc
 * @createTime 2021/12/10 20:38
 * @since
 */
public class HuffmanHelper {

    private List<SQLAuditRecordVO> baseData=new ArrayList<>();

    private Map<String,HuffmanTree> huffmanTreeMap = new HashMap<>();
    private Map<String,Iterator> iteratorHashMap = new HashMap<>();

    private int readIndex  = 0;

    public <T> void buildImmutable(String key,List<T> valueList){
        huffmanTreeMap.put(key,new ImmutableHuffmanTree<T>(valueList));
        iteratorHashMap.put(key,huffmanTreeMap.get(key).iterator());
    }

    public <T> void buildAlterable(String key,List<T> valueList){
        huffmanTreeMap.put(key,new AlterableHuffmanTree<>(valueList));
        iteratorHashMap.put(key,huffmanTreeMap.get(key).iterator());
    }

    public <T> void addAlterable(String key,List<T> valueList){
        if (huffmanTreeMap.get(key)==null){
            huffmanTreeMap.put(key,new AlterableHuffmanTree<T>(valueList));
        }else {
            huffmanTreeMap.get(key).addHuffmanTree(new AlterableHuffmanTree<T>(valueList));
        }
        iteratorHashMap.put(key,huffmanTreeMap.get(key).iterator());
    }

    public void setBaseData(List<SQLAuditRecordVO> baseData) {
        this.baseData = baseData;
    }

    public List<SQLAuditRecordVO> getBaseData() {
        return baseData;
    }

    private int num=0;

    public String buildInsterSQL(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("insert into sql_audit_record(" +
                "log_id,affected_rows,command,failed_reason,host,in_multi_query," +
                "ip,matched_rows,port,query_rows,request_end_time,request_time," +
                "response_end_time,response_time,sql_subtype,sql_type,status,user,connection_id) values");
        String collect = this.baseData.stream().map(x -> buildSQLValue(x)).collect(Collectors.joining(","));
        stringBuilder.append(collect);
        iteratorHashMap.forEach((x,y)->{
            System.out.println(x+":"+y.hasNext());
        });
        return stringBuilder.toString();
    }


    public String nextSQL(int length){
        StringBuilder stringBuilder = new StringBuilder("insert into sql_audit_record(" +
                "log_id,affected_rows,command,failed_reason,host,in_multi_query," +
                "ip,matched_rows,port,query_rows,request_end_time,request_time," +
                "response_end_time,response_time,sql_subtype,sql_type,status,user,connection_id,logic_db) values");
        int leftLength = baseData.size()-readIndex;
        length = leftLength<length?leftLength:length;
        stringBuilder.append(Stream.iterate(readIndex,n->n+1).limit(length).map(n->buildSQLValue(baseData.get(n))).collect(Collectors.joining(",")));
        readIndex=readIndex+length;
        return stringBuilder.toString();
    }

    public boolean hasNext(){
        return readIndex<baseData.size();
    }

    public String buildSQLValue(SQLAuditRecordVO sqlAuditRecordVO){
        String value = "('"+sqlAuditRecordVO.getLogId()+"',"+sqlAuditRecordVO.getAffectedRows()+",'"+iteratorHashMap.get("command").next().toString().replace("'","\"")+"'," +
                "'"+sqlAuditRecordVO.getFailedReason().replace("'","\"")+"','"+iteratorHashMap.get("host").next()+"',"+sqlAuditRecordVO.getInMultiQuery()+"," +
                "'"+iteratorHashMap.get("ip").next()+"',"+sqlAuditRecordVO.getMatchedRows()+","+sqlAuditRecordVO.getPort()+","+sqlAuditRecordVO.getQueryRows()+"," +
                ""+sqlAuditRecordVO.getRequestEndTime()+","+sqlAuditRecordVO.getRequestTime()+","+sqlAuditRecordVO.getResponseEndTime()+","+sqlAuditRecordVO.getResponseTime()+"," +
                "'"+iteratorHashMap.get("sql_subtype").next()+"','"+iteratorHashMap.get("sql_type").next()+"',"+sqlAuditRecordVO.getStatus()+"," +
                "'"+iteratorHashMap.get("user").next()+"',"+sqlAuditRecordVO.getConnectionId()+",'"+iteratorHashMap.get("logic_db").next()+"')";
//        String ip= null;
//        try {
//            ip = iteratorHashMap.get("ip").next().toString();
//        } catch (Exception e) {
//            System.out.println(num);
//        }
//        String command=iteratorHashMap.get("command").next().toString();
//        String host=iteratorHashMap.get("host").next().toString();
//        String sql_subtype=iteratorHashMap.get("sql_subtype").next().toString();
//        String sql_type=iteratorHashMap.get("sql_type").next().toString();
//        String user=iteratorHashMap.get("user").next().toString();
        //num++;
//        if (!ip.equalsIgnoreCase(sqlAuditRecordVO.getIp())){
//            //throw new HotDBManagementException(-1,num+":"+num+",correctValue:"+sqlAuditRecordVO.getIp()+",nowvalue:"+ip);
//            System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getIp()+",nowvalue:"+ip);
//        }
//        num++;
//        if (!host.equalsIgnoreCase(sqlAuditRecordVO.getHost())){
//            System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getHost()+",nowvalue:"+host);
//        }
//        if (!command.equalsIgnoreCase(sqlAuditRecordVO.getCommand())){
//            //throw new HotDBManagementException(-1,num+":"+num+",correctValue:"+sqlAuditRecordVO.getCommand()+",nowvalue:"+command);
//            System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getCommand()+",nowvalue:"+command);
//        }
//        if (!sql_subtype.equalsIgnoreCase(sqlAuditRecordVO.getSqlSubtype())){
//            throw new HotDBManagementException(-1,num+":"+num+",correctValue:"+sqlAuditRecordVO.getSqlSubtype()+",nowvalue:"+sql_subtype);
//            //System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getSqlSubtype()+",nowvalue:"+sql_subtype);
//        }
//        if (!sql_type.equalsIgnoreCase(sqlAuditRecordVO.getSqlType())){
//            System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getSqlType()+",nowvalue:"+sql_type);
//        }
//        if (!user.equalsIgnoreCase(sqlAuditRecordVO.getUser())){
//            System.out.println(num+":"+num+",correctValue:"+sqlAuditRecordVO.getUser()+",nowvalue:"+user);
//        }
//        num++;
//        System.out.println(num+":"+value);
        return  value;
    }
}
